Create attendance for employee when pressing the check in button and set the status when check in on time or...











up vote
0
down vote

favorite












I have a problem about creating a record for attendance of an employee using DATE() and TIME() with NOW() in laravel. I have the table Attendance with column to store the record into database.



        $table->increments('id');
$table->integer('user_id')->unsigned();
$table->date('attendance_date')->nullable();
$table->string('scan_in_location')->nullable();
$table->string('scan_out_location')->nullable();
$table->time('scan_in_time')->nullable();
$table->time('scan_out_time')->nullable();
$table->time('work_hour')->nullable();
$table->time('work_hour_total')->nullable();
$table->time('lunch_break')->default('01:00:00');
$table->integer('scan_in_status')->nullable();
$table->integer('scan_out_status')->nullable();
$table->timestamps();


When the employee press CHECK IN on mobile, the record to will stored in column (user_id, scan_in_time, scan_in_location, scan_in_status) but when i only do



$attendance = new Attendance();

if ($this->user->attendance()->save($attendance))
return response()->json([
'success' => true,
'attendance' => $attendance
]);
else
return response()->json([
'success' => false,
'message' => 'Sorry, check in attendance could not be added'
], 500);


This code runs smooth with the account based on user_id. But when i press Create, i want to stored (user_id, scan_in_location, scan_in_time, scan_in_status) and this code return MariaDB version



//declare variable for taking current date - time
$user_id = request('user_id');

//check attendance using user_id
$res = Attendance::select('id')
->where('user_id' , '=' , $user_id)
->where('attendance_date', '=' , DB::RAW('DATE(NOW())'))
->where('user_id' , '!=' , null)
->where('scan_in_time', '!=' , null)
->where('scan_out_time', '=', null)->get()->count();

//set status 0 - Absent, 1 - On time, 2 - Late, 3 - very late, 4 - On leave
$attendance_status = Attendance::where('user_id', '=' , $user_id)
->select(DB::RAW("CASE WHEN TIME(NOW()) >= '08:30:00' AND TIME(NOW()) < '09:00:00' THEN '2' WHEN TIME(NOW()) > '09:00:00' THEN '3' ELSE '1'))
->value('scan_in_status');

if($res > 0){
$res = DB::RAW("UPDATE attendances SET scan_out_time = NOW(),scan_out_location = ? ,work_hour = TIMESTAMPDIFF(MINUTE, scan_in_time, scan_out_time,lunch_break), work_hour_total = TIMESTAMPDIFF(MINUTE, scan_in_time, scan_out_time) WHERE `user_id` = ? AND NOT scan_in_time IS NULL AND scan_out_time IS NULL", [$user_id, $user_id, $scan_out_location]);

return response()->json(
array(
'success' => true,
'user_id' => $user_id,
'action' => 'SCAN_OUT',
'scan_out_location' => $scan_out_location,
'date' => date('Y/m/d'),
'time' => date('H:i:s')
)
);
}else{
$res = DB::RAW("UPDATE attendances SET scan_in_time = NOW(), scan_in_location = ?, scan_in_status = ?
WHERE attendance_date = DATE(NOW()) AND `user_id` = ?", [$scan_in_location, $scan_in_status, $user_id]);

return response()->json(
array(
'success' => true,
'user_id' => $user_id,
'action' => 'SCAN_IN',
'scan_in_location' => $scan_in_location,
'date' => date('Y/m/d'),
'time' => date('H:i:s')
)
);
}


Can anyone give me some advises on this check in attendance using real time DATE(NOW()) and TIME(NOW)). Thank you.










share|improve this question
























  • Still have error on MariaDB version
    – Huy Nguyen
    2 days ago










  • "SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from attendances where user_id is null limit 1' at line 1 (SQL: select CASE WHEN TIME(NOW()) >= '08:30:00' AND TIME(NOW()) < '09:00:00' THEN '2' WHEN TIME(NOW()) > '09:00:00' THEN '3' ELSE '1' from attendances where user_id is null limit 1)"
    – Huy Nguyen
    2 days ago















up vote
0
down vote

favorite












I have a problem about creating a record for attendance of an employee using DATE() and TIME() with NOW() in laravel. I have the table Attendance with column to store the record into database.



        $table->increments('id');
$table->integer('user_id')->unsigned();
$table->date('attendance_date')->nullable();
$table->string('scan_in_location')->nullable();
$table->string('scan_out_location')->nullable();
$table->time('scan_in_time')->nullable();
$table->time('scan_out_time')->nullable();
$table->time('work_hour')->nullable();
$table->time('work_hour_total')->nullable();
$table->time('lunch_break')->default('01:00:00');
$table->integer('scan_in_status')->nullable();
$table->integer('scan_out_status')->nullable();
$table->timestamps();


When the employee press CHECK IN on mobile, the record to will stored in column (user_id, scan_in_time, scan_in_location, scan_in_status) but when i only do



$attendance = new Attendance();

if ($this->user->attendance()->save($attendance))
return response()->json([
'success' => true,
'attendance' => $attendance
]);
else
return response()->json([
'success' => false,
'message' => 'Sorry, check in attendance could not be added'
], 500);


This code runs smooth with the account based on user_id. But when i press Create, i want to stored (user_id, scan_in_location, scan_in_time, scan_in_status) and this code return MariaDB version



//declare variable for taking current date - time
$user_id = request('user_id');

//check attendance using user_id
$res = Attendance::select('id')
->where('user_id' , '=' , $user_id)
->where('attendance_date', '=' , DB::RAW('DATE(NOW())'))
->where('user_id' , '!=' , null)
->where('scan_in_time', '!=' , null)
->where('scan_out_time', '=', null)->get()->count();

//set status 0 - Absent, 1 - On time, 2 - Late, 3 - very late, 4 - On leave
$attendance_status = Attendance::where('user_id', '=' , $user_id)
->select(DB::RAW("CASE WHEN TIME(NOW()) >= '08:30:00' AND TIME(NOW()) < '09:00:00' THEN '2' WHEN TIME(NOW()) > '09:00:00' THEN '3' ELSE '1'))
->value('scan_in_status');

if($res > 0){
$res = DB::RAW("UPDATE attendances SET scan_out_time = NOW(),scan_out_location = ? ,work_hour = TIMESTAMPDIFF(MINUTE, scan_in_time, scan_out_time,lunch_break), work_hour_total = TIMESTAMPDIFF(MINUTE, scan_in_time, scan_out_time) WHERE `user_id` = ? AND NOT scan_in_time IS NULL AND scan_out_time IS NULL", [$user_id, $user_id, $scan_out_location]);

return response()->json(
array(
'success' => true,
'user_id' => $user_id,
'action' => 'SCAN_OUT',
'scan_out_location' => $scan_out_location,
'date' => date('Y/m/d'),
'time' => date('H:i:s')
)
);
}else{
$res = DB::RAW("UPDATE attendances SET scan_in_time = NOW(), scan_in_location = ?, scan_in_status = ?
WHERE attendance_date = DATE(NOW()) AND `user_id` = ?", [$scan_in_location, $scan_in_status, $user_id]);

return response()->json(
array(
'success' => true,
'user_id' => $user_id,
'action' => 'SCAN_IN',
'scan_in_location' => $scan_in_location,
'date' => date('Y/m/d'),
'time' => date('H:i:s')
)
);
}


Can anyone give me some advises on this check in attendance using real time DATE(NOW()) and TIME(NOW)). Thank you.










share|improve this question
























  • Still have error on MariaDB version
    – Huy Nguyen
    2 days ago










  • "SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from attendances where user_id is null limit 1' at line 1 (SQL: select CASE WHEN TIME(NOW()) >= '08:30:00' AND TIME(NOW()) < '09:00:00' THEN '2' WHEN TIME(NOW()) > '09:00:00' THEN '3' ELSE '1' from attendances where user_id is null limit 1)"
    – Huy Nguyen
    2 days ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a problem about creating a record for attendance of an employee using DATE() and TIME() with NOW() in laravel. I have the table Attendance with column to store the record into database.



        $table->increments('id');
$table->integer('user_id')->unsigned();
$table->date('attendance_date')->nullable();
$table->string('scan_in_location')->nullable();
$table->string('scan_out_location')->nullable();
$table->time('scan_in_time')->nullable();
$table->time('scan_out_time')->nullable();
$table->time('work_hour')->nullable();
$table->time('work_hour_total')->nullable();
$table->time('lunch_break')->default('01:00:00');
$table->integer('scan_in_status')->nullable();
$table->integer('scan_out_status')->nullable();
$table->timestamps();


When the employee press CHECK IN on mobile, the record to will stored in column (user_id, scan_in_time, scan_in_location, scan_in_status) but when i only do



$attendance = new Attendance();

if ($this->user->attendance()->save($attendance))
return response()->json([
'success' => true,
'attendance' => $attendance
]);
else
return response()->json([
'success' => false,
'message' => 'Sorry, check in attendance could not be added'
], 500);


This code runs smooth with the account based on user_id. But when i press Create, i want to stored (user_id, scan_in_location, scan_in_time, scan_in_status) and this code return MariaDB version



//declare variable for taking current date - time
$user_id = request('user_id');

//check attendance using user_id
$res = Attendance::select('id')
->where('user_id' , '=' , $user_id)
->where('attendance_date', '=' , DB::RAW('DATE(NOW())'))
->where('user_id' , '!=' , null)
->where('scan_in_time', '!=' , null)
->where('scan_out_time', '=', null)->get()->count();

//set status 0 - Absent, 1 - On time, 2 - Late, 3 - very late, 4 - On leave
$attendance_status = Attendance::where('user_id', '=' , $user_id)
->select(DB::RAW("CASE WHEN TIME(NOW()) >= '08:30:00' AND TIME(NOW()) < '09:00:00' THEN '2' WHEN TIME(NOW()) > '09:00:00' THEN '3' ELSE '1'))
->value('scan_in_status');

if($res > 0){
$res = DB::RAW("UPDATE attendances SET scan_out_time = NOW(),scan_out_location = ? ,work_hour = TIMESTAMPDIFF(MINUTE, scan_in_time, scan_out_time,lunch_break), work_hour_total = TIMESTAMPDIFF(MINUTE, scan_in_time, scan_out_time) WHERE `user_id` = ? AND NOT scan_in_time IS NULL AND scan_out_time IS NULL", [$user_id, $user_id, $scan_out_location]);

return response()->json(
array(
'success' => true,
'user_id' => $user_id,
'action' => 'SCAN_OUT',
'scan_out_location' => $scan_out_location,
'date' => date('Y/m/d'),
'time' => date('H:i:s')
)
);
}else{
$res = DB::RAW("UPDATE attendances SET scan_in_time = NOW(), scan_in_location = ?, scan_in_status = ?
WHERE attendance_date = DATE(NOW()) AND `user_id` = ?", [$scan_in_location, $scan_in_status, $user_id]);

return response()->json(
array(
'success' => true,
'user_id' => $user_id,
'action' => 'SCAN_IN',
'scan_in_location' => $scan_in_location,
'date' => date('Y/m/d'),
'time' => date('H:i:s')
)
);
}


Can anyone give me some advises on this check in attendance using real time DATE(NOW()) and TIME(NOW)). Thank you.










share|improve this question















I have a problem about creating a record for attendance of an employee using DATE() and TIME() with NOW() in laravel. I have the table Attendance with column to store the record into database.



        $table->increments('id');
$table->integer('user_id')->unsigned();
$table->date('attendance_date')->nullable();
$table->string('scan_in_location')->nullable();
$table->string('scan_out_location')->nullable();
$table->time('scan_in_time')->nullable();
$table->time('scan_out_time')->nullable();
$table->time('work_hour')->nullable();
$table->time('work_hour_total')->nullable();
$table->time('lunch_break')->default('01:00:00');
$table->integer('scan_in_status')->nullable();
$table->integer('scan_out_status')->nullable();
$table->timestamps();


When the employee press CHECK IN on mobile, the record to will stored in column (user_id, scan_in_time, scan_in_location, scan_in_status) but when i only do



$attendance = new Attendance();

if ($this->user->attendance()->save($attendance))
return response()->json([
'success' => true,
'attendance' => $attendance
]);
else
return response()->json([
'success' => false,
'message' => 'Sorry, check in attendance could not be added'
], 500);


This code runs smooth with the account based on user_id. But when i press Create, i want to stored (user_id, scan_in_location, scan_in_time, scan_in_status) and this code return MariaDB version



//declare variable for taking current date - time
$user_id = request('user_id');

//check attendance using user_id
$res = Attendance::select('id')
->where('user_id' , '=' , $user_id)
->where('attendance_date', '=' , DB::RAW('DATE(NOW())'))
->where('user_id' , '!=' , null)
->where('scan_in_time', '!=' , null)
->where('scan_out_time', '=', null)->get()->count();

//set status 0 - Absent, 1 - On time, 2 - Late, 3 - very late, 4 - On leave
$attendance_status = Attendance::where('user_id', '=' , $user_id)
->select(DB::RAW("CASE WHEN TIME(NOW()) >= '08:30:00' AND TIME(NOW()) < '09:00:00' THEN '2' WHEN TIME(NOW()) > '09:00:00' THEN '3' ELSE '1'))
->value('scan_in_status');

if($res > 0){
$res = DB::RAW("UPDATE attendances SET scan_out_time = NOW(),scan_out_location = ? ,work_hour = TIMESTAMPDIFF(MINUTE, scan_in_time, scan_out_time,lunch_break), work_hour_total = TIMESTAMPDIFF(MINUTE, scan_in_time, scan_out_time) WHERE `user_id` = ? AND NOT scan_in_time IS NULL AND scan_out_time IS NULL", [$user_id, $user_id, $scan_out_location]);

return response()->json(
array(
'success' => true,
'user_id' => $user_id,
'action' => 'SCAN_OUT',
'scan_out_location' => $scan_out_location,
'date' => date('Y/m/d'),
'time' => date('H:i:s')
)
);
}else{
$res = DB::RAW("UPDATE attendances SET scan_in_time = NOW(), scan_in_location = ?, scan_in_status = ?
WHERE attendance_date = DATE(NOW()) AND `user_id` = ?", [$scan_in_location, $scan_in_status, $user_id]);

return response()->json(
array(
'success' => true,
'user_id' => $user_id,
'action' => 'SCAN_IN',
'scan_in_location' => $scan_in_location,
'date' => date('Y/m/d'),
'time' => date('H:i:s')
)
);
}


Can anyone give me some advises on this check in attendance using real time DATE(NOW()) and TIME(NOW)). Thank you.







php laravel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 12:59









Saumini Navaratnam

4,26911542




4,26911542










asked Nov 10 at 12:17









Huy Nguyen

115




115












  • Still have error on MariaDB version
    – Huy Nguyen
    2 days ago










  • "SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from attendances where user_id is null limit 1' at line 1 (SQL: select CASE WHEN TIME(NOW()) >= '08:30:00' AND TIME(NOW()) < '09:00:00' THEN '2' WHEN TIME(NOW()) > '09:00:00' THEN '3' ELSE '1' from attendances where user_id is null limit 1)"
    – Huy Nguyen
    2 days ago


















  • Still have error on MariaDB version
    – Huy Nguyen
    2 days ago










  • "SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from attendances where user_id is null limit 1' at line 1 (SQL: select CASE WHEN TIME(NOW()) >= '08:30:00' AND TIME(NOW()) < '09:00:00' THEN '2' WHEN TIME(NOW()) > '09:00:00' THEN '3' ELSE '1' from attendances where user_id is null limit 1)"
    – Huy Nguyen
    2 days ago
















Still have error on MariaDB version
– Huy Nguyen
2 days ago




Still have error on MariaDB version
– Huy Nguyen
2 days ago












"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from attendances where user_id is null limit 1' at line 1 (SQL: select CASE WHEN TIME(NOW()) >= '08:30:00' AND TIME(NOW()) < '09:00:00' THEN '2' WHEN TIME(NOW()) > '09:00:00' THEN '3' ELSE '1' from attendances where user_id is null limit 1)"
– Huy Nguyen
2 days ago




"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'from attendances where user_id is null limit 1' at line 1 (SQL: select CASE WHEN TIME(NOW()) >= '08:30:00' AND TIME(NOW()) < '09:00:00' THEN '2' WHEN TIME(NOW()) > '09:00:00' THEN '3' ELSE '1' from attendances where user_id is null limit 1)"
– Huy Nguyen
2 days ago

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238873%2fcreate-attendance-for-employee-when-pressing-the-check-in-button-and-set-the-sta%23new-answer', 'question_page');
}
);

Post as a guest





































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238873%2fcreate-attendance-for-employee-when-pressing-the-check-in-button-and-set-the-sta%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

The Sandy Post

Danny Elfman

Pages that link to "Head v. Amoskeag Manufacturing Co."