API how to get is user is clocked in or clocked out - getUserPunch

Discussion for TimeTrex open source community developers.
Post Reply
lumirl
Posts: 6
Joined: Mon Jul 29, 2019 8:16 am

API how to get is user is clocked in or clocked out - getUserPunch

Post by lumirl »

Hello,
I am trying to get if a user has clocked in or clocked out.
From reading forums I understood that the api function getUserPunch should tell me this.
But I can't make it work.
I can add punches for a user. And because punches are set for automatic type (status_id=0) need to find out if the punch was clock in or clock out.

echo "user ID is: $id\n";
echo "username is: $username\n";
$punch_data2 = array(
#'id' => $id,
'user_name' => "$username",
#'user_id' => "NULL",
#'epoch' => "NULL",
'station_id' => "NULL",
#'company_id' => "NULL",
#'station_id' => '(ANY, ANY)',
);

$result = postToURL( buildURL( 'APIPunch', 'getUserPunch' ), array( $punch_data2 ) );
print $result;

=============================================================================================
user ID is: 11e9bcd7-65dd-12c0-9e76-6506ec593a72
username is: test
==============================================================
Posting data to URL: http://localhost/timetrex/api/json/api. ... 370553740f
POST Data: json=%5B%7B%22user_name%22%3A%22test%22%2C%22station_id%22%3A%22NULL%22%7D%5D
--------------------------------------------------------------
Returned:
IsValid: NO
Code: VALIDATION
Description: INVALID DATA
Details:
Row: 0
--Field: user_name
----Message: You are not authorized to punch in or out from this station!
===============================================================================================

Any help appretiated!
mikeb
Posts: 709
Joined: Thu Jul 27, 2006 11:58 am

Re: API how to get is user is clocked in or clocked out - getUserPunch

Post by mikeb »

You haven't provided any details for the actual use-case that you are trying to solve, so its difficult for us to offer a helpful answer.

getUserPunch() would normally only be used if you are about to punch an employee in/out and want to get the most likely default settings for them to use. It also checks that they meet any station restrictions and are allowed to punch in/out from a given station.

If you simply want to know if a given employee or group of employees is punched in/out, you should use the Who's In/Out Summary report instead. This is referred to as the ActiveShiftReport from the API.
TimeTrex Community Edition is developed and supported by volunteers.
Help motivate us to continue by showing your appreciation!
lumirl
Posts: 6
Joined: Mon Jul 29, 2019 8:16 am

Re: API how to get is user is clocked in or clocked out - getUserPunch

Post by lumirl »

Thank you for your reply.
I am trying to add punch in and I want to get a reply if the punch was a clock in or clock out.
The punch in is set for status_id=0 - automatic.
So after they punch in I want to get a reply for the same user if he is clocked in or clocked out.
So what APi method can I use for this?
If you can provide an php example that would be great.
mikeb
Posts: 709
Joined: Thu Jul 27, 2006 11:58 am

Re: API how to get is user is clocked in or clocked out - getUserPunch

Post by mikeb »

If you simply want to add punches from the API, there is an example of how to do that at the bottom of the following page, which allows you to explicitly set if its an In/Out punch:
https://www.timetrex.com/workforce-management-api
TimeTrex Community Edition is developed and supported by volunteers.
Help motivate us to continue by showing your appreciation!
lumirl
Posts: 6
Joined: Mon Jul 29, 2019 8:16 am

Re: API how to get is user is clocked in or clocked out - getUserPunch

Post by lumirl »

No - I said that I know how to add punch and adding punches works fine.
I need to know whether the punch was IN or OUT for that user who did the punch.
Because its set as automatic punch in type.
mikeb
Posts: 709
Joined: Thu Jul 27, 2006 11:58 am

Re: API how to get is user is clocked in or clocked out - getUserPunch

Post by mikeb »

How exactly are you adding the punch initially?
TimeTrex Community Edition is developed and supported by volunteers.
Help motivate us to continue by showing your appreciation!
lumirl
Posts: 6
Joined: Mon Jul 29, 2019 8:16 am

Re: API how to get is user is clocked in or clocked out - getUserPunch

Post by lumirl »

I have solved my issue already.
This is basically the whole code without the initial functions to login and posturl.
===========================================
$arguments = array( 'filter_data' => array(
'phone_id' => $argv[1],
)
);
$user_data = postToURL( buildURL( 'APIUser', 'getUser' ), array( $arguments ) );

#
$idd = array_column($user_data, 'id');
$usernamee = array_column($user_data, 'user_name');
$fname = array_column($user_data, 'first_name');
$lname = array_column($user_data, 'last_name');
$phone = array_column($user_data, 'phone_id');
$cislo_arr = array_search($argv[1],$phone);
#echo "cislo_arr: $cislo_arr \n";

$id = $idd[$cislo_arr];
$jmeno = $fname[$cislo_arr];
$prijmeni = $lname[$cislo_arr];
$username = $usernamee[$cislo_arr];
echo "prijmeni: $prijmeni \nid: $id\n$username \n";

$punch_data = array(
'user_id' => $id,

'type_id' => 10, //Normal
//status ID=0 means punch will automatically determine if its login or logout
'status_id' => 0, //Auto
'time_stamp' => time(),
);

$result = postToURL( buildURL( 'APIPunch', 'setPunch' ), array( $punch_data ) );
if ( $result !== FALSE ) {
//if the response is ok lets figure out what was the last punch for the user. filter last day punches and from the latest find out if it was punch in or punch out
$dnesje = strtotime("-12 hours");
$dnesje2 = strtotime("now");
$punch_data2 = array('filter_data' => array (
'user_id' => $id,
'start_date' => $dnesje,
'end_date' => $dnesje2,
));
$result2 = postToURL( buildURL( 'APIPunch', 'getPunch' ), array( $punch_data2 ) );
#print_r ($result2);
$maxkey = max(array_keys($result2));
echo "maxkey is: $maxkey \n";
$inoutt = array_column($result2, 'status');
$inout = $inoutt[$maxkey];
echo "The last punch was: $inout\n";
if ($inout == "In") {
echo "User $jmeno $prijmeni successfully logged in.\n";
} else {
echo "User $jmeno $prijmeni successfully logged out .\n";
}

$insert_id = $result; //Get employees new ID on success.
} else {
echo "Punch save failed.\n";
print $result; //Show error messages
}
Post Reply