# CheckInCheckOutController

app / Http / Controllers / CheckInCheckOutController.php

# index

public function index()
    {
        if (!Auth::user()->can('View Attendance')){
            return abort('404');
        }
        return view('Attendance.index');

    }
1
2
3
4
5
6
7
8

# pdfDownload

public function pdfDownload(){
        $attendances = CheckInCheckOut::with('employeeCheckInfo')->get();
        $pdf = PDF::loadView('PDF.attendance', ['attendances'=>$attendances]);
//        return $pdf->stream('attendance.pdf');
        return $pdf->download('attendance.pdf');
}
1
2
3
4
5
6

# checkInCheckOut

public function checkInCheckOut(){
//        $company = CompanySetting::findOrfail(1);
        $hashQR_code = Hash::make(date('Y-m-d'));
        return view('CheckInCheckOut.CheckInCheckOut', compact('hashQR_code'));
    }
1
2
3
4
5

# checkInOut

public function checkInOut(Request $request){
    $today = now()->format('D');
    if ($today == "Sat" || $today == "Sun"){
        return[
            'status' => 'Fail',
            'message' => 'Today is weekend.'
        ];
    }
    $employee = User::where('pin_code', $request->pin_code)->first();
    if (!$employee){
        return[
            'status' => 'Fail',
            'message' => 'Pin Code Is Not Found.'
        ];
    }
    $datas = CheckInCheckOut::firstOrCreate(
        [
            'user_id' => $employee->id,
            'Indate' => now()->format('Y-m-d')
        ]
    );

    if (!is_null($datas->checkIn_time) && !is_null($datas->checkOut_time)){
        return[
            'status' => 'Fail',
            'message' => 'You have done Check In and Check Out at today.',
        ];
    }

    if (is_null($datas->checkIn_time)){
        $datas->checkIn_time = now();
        $message = 'Successfully Check In At '.now();
    }else{
        if (is_null($datas->checkOut_time)){
            $datas->checkOut_time = now();
            $message = 'Successfully Check Out At '.now();
        }
    }
    $datas->update();
    return[
        'status' => 'Success',
        'message' => $message,
    ];
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

# serverSideData

public function serverSideDatas(){
        if (!Auth::user()->can('View Attendance')){
            return abort('404');
        }
        $attendances = CheckInCheckOut::with('employeeCheckInfo');
        return Datatables::of($attendances)
            ->filterColumn('employee_name', function ($query, $keyword){
                $query->whereHas('employeeCheckInfo', function ($q) use ($keyword){
                    $q->where('name', 'like', "%$keyword%");
                });
            })
            ->addColumn('employee_name', function ($each){
                return $each->employeeCheckInfo ? $each->employeeCheckInfo->name : '-';
            })
            ->addColumn('action', function ($each){
                $edit = '';
                $delete= '';
                if (Auth::user()->can('Edit Attendance')){
                    $edit = '<a href="'. route('attendance.edit', $each->id) .'" class="btn btn-outline-warning me-2"><i class="fa-solid fa-pen-to-square"></i></a>';
                }
                if (Auth::user()->can('Delete Attendance')){
                    $delete = '<a href="#" class="btn btn-outline-danger delete-btn"
                                            data-id="'.$each->id.'"
                                            data-name="'.$each->employeeCheckInfo->name.'">
                                    <i class="fa-solid fa-trash-alt"></i>
                                </a>';
                }
                $detail = '<a href="'. route('attendance.show', $each->id) .'" class="btn btn-outline-primary me-2"><i class="fa-solid fa-circle-info"></i></a>';
                return '<div class="d-flex">'. $detail. $edit . $delete.'</div>';
            })
            ->addColumn('plus_icon', function ($each){
                return null;
            })
            ->rawColumns(['action']) // for HTML return
            ->make(true);
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

# create

public function create()
    {
        if (!Auth::user()->can('Create Attendance')){
            return abort('404');
        }
        $employees = User::orderBy('name')->get();
        return view('Attendance.create', ['employees' => $employees]);
    }
1
2
3
4
5
6
7
8

# store

public function store(StoreCheckInCheckOutRequest $request)
    {
        if (!Auth::user()->can('Create Attendance')){
            return abort('404');
        }
        if (CheckInCheckOut::where('user_id', $request->user)
                            ->where('Indate', $request->date)
                            ->exists()
            ){
            return redirect()
                ->back()
                ->with('Fail','Already Defined.')->withInput();
        }
        $attendance = new CheckInCheckOut();
        $attendance->user_id = $request->user;
        $attendance->Indate = $request->date;
        $attendance->checkIn_time = $request->date." ".$request->checkIn;
        $attendance->checkOut_time = $request->date." ".$request->checkOut;
        $attendance->save();
        return redirect()
            ->route('attendance.index')
            ->with('create', 'Attendance is successfully created.');
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# edit

public function edit($id)
    {
        if (!Auth::user()->can('Edit Attendance')){
            return abort('404');
        }
        $checkInCheckOut = CheckInCheckOut::findOrfail($id);
        $employees = User::orderBy('name')->get();
        return view('Attendance.edit', [
                                'checkInCheckOut'=>$checkInCheckOut,
                                'employees'=>$employees
                            ]);
    }
1
2
3
4
5
6
7
8
9
10
11
12

# update

public function update(UpdateCheckInCheckOutRequest $request, $id)
    {
        if (!Auth::user()->can('Edit Attendance')){
            return abort('404');
        }

        $attendance = CheckInCheckOut::findOrfail($id);

        if (CheckInCheckOut::where('user_id', $request->user)
                            ->where('Indate', $request->date)
                            ->where('id', '!=', $attendance->id)
                            ->exists()
        ){
            return redirect()
                ->back()
                ->with('Fail','Already Defined.')->withInput();
        }

        $attendance->user_id = $request->user;
        $attendance->Indate = $request->date;
        $attendance->checkIn_time = $request->date." ".$request->checkIn;
        $attendance->checkOut_time = $request->date." ".$request->checkOut;
        $attendance->update();
        return redirect()
            ->route('attendance.index')
            ->with('create', 'Attendance is successfully Updated.');
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# destroy

public function destroy($id)
    {
        $attendance = CheckInCheckOut::findOrfail($id);
        $attendance->delete();
    }
1
2
3
4
5

# Code Overview

<?php

namespace App\Http\Controllers;

use App\Http\Requests\StoreCheckInCheckOutRequest;
use App\Http\Requests\UpdateCheckInCheckOutRequest;
use App\Models\CheckInCheckOut;
use App\Models\CompanySetting;
use App\Models\Department;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Yajra\DataTables\DataTables;
use PDF;

class CheckInCheckOutController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        if (!Auth::user()->can('View Attendance')){
            return abort('404');
        }
        return view('Attendance.index');

    }

    public function pdfDownload(){
        $attendances = CheckInCheckOut::with('employeeCheckInfo')->get();
        $pdf = PDF::loadView('PDF.attendance', ['attendances'=>$attendances]);
//        return $pdf->stream('attendance.pdf');
        return $pdf->download('attendance.pdf');
    }

    public function checkInCheckOut(){
//        $company = CompanySetting::findOrfail(1);
        $hashQR_code = Hash::make(date('Y-m-d'));
        return view('CheckInCheckOut.CheckInCheckOut', compact('hashQR_code'));
    }

    public function checkInOut(Request $request){
        $today = now()->format('D');
        if ($today == "Sat" || $today == "Sun"){
            return[
                'status' => 'Fail',
                'message' => 'Today is weekend.'
            ];
        }
        $employee = User::where('pin_code', $request->pin_code)->first();
        if (!$employee){
            return[
                'status' => 'Fail',
                'message' => 'Pin Code Is Not Found.'
            ];
        }
        $datas = CheckInCheckOut::firstOrCreate(
            [
                'user_id' => $employee->id,
                'Indate' => now()->format('Y-m-d')
            ]
        );

        if (!is_null($datas->checkIn_time) && !is_null($datas->checkOut_time)){
            return[
                'status' => 'Fail',
                'message' => 'You have done Check In and Check Out at today.',
            ];
        }

        if (is_null($datas->checkIn_time)){
            $datas->checkIn_time = now();
            $message = 'Successfully Check In At '.now();
        }else{
            if (is_null($datas->checkOut_time)){
                $datas->checkOut_time = now();
                $message = 'Successfully Check Out At '.now();
            }
        }
        $datas->update();
        return[
            'status' => 'Success',
            'message' => $message,
        ];
    }

    public function serverSideDatas(){
        if (!Auth::user()->can('View Attendance')){
            return abort('404');
        }
        $attendances = CheckInCheckOut::with('employeeCheckInfo');
        return Datatables::of($attendances)
            ->filterColumn('employee_name', function ($query, $keyword){
                $query->whereHas('employeeCheckInfo', function ($q) use ($keyword){
                    $q->where('name', 'like', "%$keyword%");
                });
            })
            ->addColumn('employee_name', function ($each){
                return $each->employeeCheckInfo ? $each->employeeCheckInfo->name : '-';
            })
            ->addColumn('action', function ($each){
                $edit = '';
                $delete= '';
                if (Auth::user()->can('Edit Attendance')){
                    $edit = '<a href="'. route('attendance.edit', $each->id) .'" class="btn btn-outline-warning me-2"><i class="fa-solid fa-pen-to-square"></i></a>';
                }
                if (Auth::user()->can('Delete Attendance')){
                    $delete = '<a href="#" class="btn btn-outline-danger delete-btn"
                                            data-id="'.$each->id.'"
                                            data-name="'.$each->employeeCheckInfo->name.'">
                                    <i class="fa-solid fa-trash-alt"></i>
                                </a>';
                }
                $detail = '<a href="'. route('attendance.show', $each->id) .'" class="btn btn-outline-primary me-2"><i class="fa-solid fa-circle-info"></i></a>';
                return '<div class="d-flex">'. $detail. $edit . $delete.'</div>';
            })
            ->addColumn('plus_icon', function ($each){
                return null;
            })
            ->rawColumns(['action']) // for HTML return
            ->make(true);
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        if (!Auth::user()->can('Create Attendance')){
            return abort('404');
        }
        $employees = User::orderBy('name')->get();
        return view('Attendance.create', ['employees' => $employees]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \App\Http\Requests\StoreCheckInCheckOutRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreCheckInCheckOutRequest $request)
    {
        if (!Auth::user()->can('Create Attendance')){
            return abort('404');
        }
        if (CheckInCheckOut::where('user_id', $request->user)
                            ->where('Indate', $request->date)
                            ->exists()
            ){
            return redirect()
                ->back()
                ->with('Fail','Already Defined.')->withInput();
        }
        $attendance = new CheckInCheckOut();
        $attendance->user_id = $request->user;
        $attendance->Indate = $request->date;
        $attendance->checkIn_time = $request->date." ".$request->checkIn;
        $attendance->checkOut_time = $request->date." ".$request->checkOut;
        $attendance->save();
        return redirect()
            ->route('attendance.index')
            ->with('create', 'Attendance is successfully created.');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\CheckInCheckOut  $checkInCheckOut
     * @return \Illuminate\Http\Response
     */
    public function show(CheckInCheckOut $checkInCheckOut)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\CheckInCheckOut  $checkInCheckOut
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        if (!Auth::user()->can('Edit Attendance')){
            return abort('404');
        }
        $checkInCheckOut = CheckInCheckOut::findOrfail($id);
        $employees = User::orderBy('name')->get();
        return view('Attendance.edit', [
                                'checkInCheckOut'=>$checkInCheckOut,
                                'employees'=>$employees
                            ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \App\Http\Requests\UpdateCheckInCheckOutRequest  $request
     * @param  \App\Models\CheckInCheckOut  $checkInCheckOut
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateCheckInCheckOutRequest $request, $id)
    {
        if (!Auth::user()->can('Edit Attendance')){
            return abort('404');
        }

        $attendance = CheckInCheckOut::findOrfail($id);

        if (CheckInCheckOut::where('user_id', $request->user)
                            ->where('Indate', $request->date)
                            ->where('id', '!=', $attendance->id)
                            ->exists()
        ){
            return redirect()
                ->back()
                ->with('Fail','Already Defined.')->withInput();
        }

        $attendance->user_id = $request->user;
        $attendance->Indate = $request->date;
        $attendance->checkIn_time = $request->date." ".$request->checkIn;
        $attendance->checkOut_time = $request->date." ".$request->checkOut;
        $attendance->update();
        return redirect()
            ->route('attendance.index')
            ->with('create', 'Attendance is successfully Updated.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\CheckInCheckOut  $checkInCheckOut
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $attendance = CheckInCheckOut::findOrfail($id);
        $attendance->delete();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
Last Updated: 6/1/2022, 8:08:37 AM